<script setup>
import { Menu as IconMenu, Setting, Star } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// import { logoutAPI } from '~/api/user'

const activeIndex = ref('/profile/userInfo')
const route = useRoute()
const router = useRouter()
const centerDialogVisible = ref(false)

function setCurrentRoute() {
  activeIndex.value = route.path
}

function openDia() {
  centerDialogVisible.value = true
}

async function handleLogout() {
  centerDialogVisible.value = false
  await localStorage.clear()
  router.push('/login')
  // await logoutAPI().then((res) => {
  //   if (res.code === 200) {

  //   }
  // })
}

watch(
  () => activeIndex,
  (value, oldValue) => {
    setCurrentRoute()
    console.log(value, oldValue)
  },
  { immediate: true },
)
</script>

<template>
  <div>
    <el-row class="h-full container">
      <el-col :span="12" class="h-full">
        <el-menu :default-active="activeIndex" class="h-full" :router="true">
          <el-menu-item index="/profile/userInfo">
            <template #title>
              <el-icon><IconMenu /></el-icon>
              <span>我的信息</span>
            </template>
          </el-menu-item>
          <el-menu-item index="/profile/myReserve">
            <el-icon><Memo /></el-icon>
            <span>预定记录</span>
          </el-menu-item>
          <el-menu-item index="/profile/myOrder">
            <el-icon><ChatLineSquare /></el-icon>
            <span>我的订单</span>
          </el-menu-item>
          <el-menu-item index="/profile/myStar">
            <el-icon><Star /></el-icon>
            <span>我的收藏</span>
          </el-menu-item>
          <el-menu-item index="/profile/settings">
            <el-icon><Setting /></el-icon>
            <span>修改密码</span>
          </el-menu-item>
          <el-menu-item @click="openDia">
            <el-icon><ArrowUp /></el-icon>
            <span>退出登录</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="12" class="h-full">
        <div class="h-full w-full">
          <router-view />
        </div>
      </el-col>
    </el-row>
    <el-dialog v-model="centerDialogVisible" title="警告" width="500" center>
      <span class="text-center">
        您确定要退出登录吗？
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="centerDialogVisible = false">
            取消
          </el-button>
          <el-button type="danger" @click="handleLogout">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 33%;
  padding-left: 0;
  height: 500px;
}
</style>
